package com.xszx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.entity.TDept;
import com.xszx.mapper.UserDeptMapper;
import com.xszx.service.TDeptService;
import com.xszx.mapper.TDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 34129
* @description 针对表【t_dept】的数据库操作Service实现
* @createDate 2024-01-08 20:40:47
*/
@Service
public class TDeptServiceImpl extends ServiceImpl<TDeptMapper, TDept>
    implements TDeptService{

    @Autowired
    private TDeptMapper deptMapper;

    @Autowired
    private UserDeptMapper userDeptMapper;

    /**
     * 遍历dept列表
     * @return
     */
    @Override
    public List<TDept> selectDepts() {
       List<TDept> depts =  deptMapper.selectDept();
        return depts;
    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @Override
    public int addDept(TDept dept) {
        int i = deptMapper.insert(dept);
        return i;
    }

    /**
     * 删除部门根据id
     * @param id
     * @return
     */
    @Transactional
    public int deleteById(Long id){
        int i = deptMapper.deleteById(id);
        //如果删除成功
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("did",id);

        if (i>0){
            //删除中间表数据，根据did去中间表删除数据
           userDeptMapper.deleteByMap(map);
           return 1;
        }else {
            //删除失败返回-1
            return -1;
        }
    }

    @Override
    public int updateDept(TDept dept) {
        int i = deptMapper.updateById(dept);
        return i;
    }

    @Override
    public TDept getDeptByName(String dname) {
        TDept dept = deptMapper.getDeptByDname(dname);
        return dept;
    }

    @Override
    public TDept getDeptByDid(Long did) {
        TDept dept = deptMapper.getDeptByDid(did);
        return dept;
    }

}




